package cn.tedu.mintmall.service.Impl;

import cn.tedu.mintmall.dto.UserLoginDTO;
import cn.tedu.mintmall.entity.User;
import cn.tedu.mintmall.exception.MintmallServiceException;
import cn.tedu.mintmall.mapper.UserMapper;
import cn.tedu.mintmall.restful.ResponseCode;
import cn.tedu.mintmall.service.IUserLongService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class userLoginImpl implements IUserLongService {
    @Autowired
    UserMapper userMapper;
    @Override
    public void addUser(UserLoginDTO userLoginDTO) {
        User user=new User();
        BeanUtils.copyProperties(userLoginDTO,user);
        user.setGmtCreate(LocalDateTime.now());
        user.setGmtModified(LocalDateTime.now());
        userMapper.insertOne(user);
    }

    @Override
    public void userLogin(String userName, String password) {
        User user = userMapper.selectByLogin(userName);
        if (user!=null){
            String existPassword= user.getUserPassword();
            if (existPassword.equals(password)){
                log.info("登陆成功!");
            }else{
                throw new MintmallServiceException(ResponseCode.UNAUTHORIZED,"密码错误");
            }
        }
        throw new MintmallServiceException(ResponseCode.BAD_REQUEST,"用户名不存在");
    }
}
